# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

import coin.exchange.base.kr_rest.private_client_base as privcb
import coin.exchange.quoinex_v2.kr_rest.native_private_client as nprivc
from coin.exchange.quoinex_v2.kr_rest.product import QuoinexProduct
from coin.exchange.quoinex_v2.kr_rest.currency import QuoinexCurrency

import coin.proto.coin_order_enums_pb2 as coin_order
from coin.proto.coin_query_pb2 import (AccountBalance,
                                       CurrencyBalance,
                                       ProductFillElement,
                                       ProductFillBundle,
                                       ProductOrderElement,
                                       ProductOrderBundle)


def _get_order_side(side: str):
  if side == 'sell':
    side_proto = coin_order.SELL_ORDER
  elif side == 'buy':
    side_proto = coin_order.BUY_ORDER
  else:
    raise ValueError('Invalid order side %s' % side)
  return side_proto


class QuoinexPrivateParser(object):
  @staticmethod
  def parse_balance(update_msg):
    # [{'balance': '1.00895', 'currency': 'SGD'},
    #  {'balance': '0.0035', 'currency': 'USD'},
    #  {'balance': '0.0', 'currency': 'EUR'},
    #  {'balance': '0.0', 'currency': 'HKD'},
    #  {'balance': '0.0', 'currency': 'AUD'},
    #  {'balance': '0.0', 'currency': 'CNY'},
    #  {'balance': '0.0', 'currency': 'INR'},
    #  {'balance': '0.0', 'currency': 'PHP'},
    #  {'balance': '0.0', 'currency': 'IDR'},
    #  {'balance': '167323.80119', 'currency': 'JPY'},
    #  {'balance': '0.37475794', 'currency': 'BTC'},
    #  {'balance': '0.0', 'currency': 'ETH'},
    #  {'balance': '0.00000191', 'currency': 'BCH'},
    #  {'balance': '0.0', 'currency': 'DASH'},
    #  {'balance': '0.0', 'currency': 'QTUM'},
    #  {'balance': '0.0', 'currency': 'UBTC'},
    #  {'balance': '0.0', 'currency': 'XRP'},
    #  {'balance': '0.0', 'currency': 'RKT'}]
    currency_balance_list = []
    for balance_info in update_msg:
      currency = QuoinexCurrency.FromStrNativeCurrencyNothrow(balance_info['currency'])
      if currency is None:
        continue
      balance = float(balance_info['balance'])
      currency_balance = CurrencyBalance(
          currency=currency.currency,
          currency_native=currency.native_currency,
          available=balance,
          hold=0,
          total=balance,
      )
      currency_balance_list.append(currency_balance)

    return AccountBalance(exchange='Quoinex',
                          market_type='Spot',
                          each_balance=currency_balance_list)


class QuoinexPrivateClient(privcb.PrivateClientBase):
  ProductType = QuoinexProduct

  def __init__(self, key_file):
    privcb.PrivateClientBase.__init__(self)
    self.nprivc = nprivc.QuoinexNativePrivateClient(key_file=key_file)

  def query_account_balance_impl(self):
    update = self.nprivc.query_get_all_account_balances()
    update.msg = QuoinexPrivateParser.parse_balance(update.msg)
    return update

  def query_fills_impl(self, product):
    raise NotImplementedError()

  def query_list_orders_impl(self, product):
    raise NotImplementedError()

  def query_submit_impl(self, product, side, price, qty):
    raise NotImplementedError()

  def query_cancel_product_impl(self, product):
    raise NotImplementedError()
